﻿Namespace GUI

    Public Structure MirItemDesc
        Dim info As EvoLib.Protocol.ItemInfo
        Dim alphacol As Color
    End Structure

    Namespace MouseDragItem

        Module mMouseDragItem

            Public MouseDragItemDesc As MirItemDesc
            Dim XOffset As Integer, YOffset As Integer
            Public CellXOffset As Integer, CellYOffset As Integer
            Dim mShow As Boolean
            Dim mReturnContainer As Object
            Dim bForceAddedItem As Boolean
            Public mOrigin As EvoLib.Protocol.ItemLocation

            Public Sub SetMouseDragItem(ByRef desc As MirItemDesc, ByRef returncontainer As Object, ByRef origin As EvoLib.Protocol.ItemLocation, ByRef xoff As Integer, ByRef yoff As Integer, ByRef cellxoff As Integer, ByRef cellyoff As Integer, Optional ByRef forceaddeditem As Boolean = False)

                MouseDragItemDesc = desc
                XOffset = xoff
                YOffset = yoff
                CellXOffset = cellxoff
                CellYOffset = cellyoff
                mReturnContainer = returncontainer
                mShow = True
                bForceAddedItem = forceaddeditem
                mOrigin = origin

            End Sub

            Public Sub RemoveMouseDragItem()

                mShow = False

            End Sub

            Public Sub ReturnMouseDragItem()

                If bForceAddedItem Then
                    mReturnContainer.AddMouseDragItem()
                Else
                    mReturnContainer.ServerAddItem(MouseDragItemDesc.info)
                End If

            End Sub

            Public Sub AddToDragParent(ByRef item As EvoLib.Protocol.ItemInfo)

                mReturnContainer.ServerAddItem(item)

            End Sub

            Public ReadOnly Property HasMouseDragItem()
                Get
                    Return mShow
                End Get
            End Property

            Public Sub DrawMouseDragItem(ByRef mode As Globals.DrawingMode)

                If mShow Then

                    If mode = Globals.DrawingMode.Normal Then
                        Libs.Inventory.Image(MouseDragItemDesc.info.Base.INT_LOOKS).Draw(Mouse.XCoord - XOffset, Mouse.YCoord - YOffset, 0.01)
                    ElseIf mode = Globals.DrawingMode.Tinted Then
                        DirectX.GameSprite.Begin(Microsoft.DirectX.Direct3D.SpriteFlags.AlphaBlend)
                        Engine.SetDrawModeTinted(1.0, MouseDragItemDesc.alphacol)
                        Libs.Inventory.Image(MouseDragItemDesc.info.Base.INT_LOOKS).DrawAlpha(Mouse.XCoord - XOffset, Mouse.YCoord - YOffset, 0.01)
                        DirectX.GameSprite.End()
                    End If

                End If

            End Sub

        End Module

    End Namespace

    Friend Class MirItemContainer
        Implements Windows.GUIObj

        Public Event AddedItem(ByRef item As EvoLib.Protocol.ItemInfo, skipupdate As Boolean)
        Public Event RemovedItem(ByRef item As EvoLib.Protocol.ItemInfo)
        Public Event RefreshingSurfaces(ByRef surf As Microsoft.DirectX.Direct3D.Surface, ByRef tex As Microsoft.DirectX.Direct3D.Texture)
        Public Event OnSelectedItemChange()

        Public Structure MirItemContainerItem
            Dim ItemDesc As MirItemDesc
            Dim pos As Point
            Dim dimensions As Size
        End Structure

        Dim mWnd As MirWindow

        Dim mName As String

        Dim mPosX As Integer, mPosY As Integer
        Dim mWidth As Integer, mHeight As Integer
        Dim mCellSize As Integer

        Dim mMaxYVal As Integer

        Dim mRectGrid As Rectangle

        Dim bIsFilled(,) As Boolean

        Dim lstItems As New List(Of MirItemContainerItem)

        Public mYScroll As Integer

        Dim mSurf As Microsoft.DirectX.Direct3D.Surface
        Dim mTex As Microsoft.DirectX.Direct3D.Texture
        Dim mGridType As EvoLib.Protocol.ItemLocation

        Dim hoverx As Integer = -1
        Dim hovery As Integer = -1
        Dim hoverw As Integer, hoverh As Integer
        Dim hoverisover As Boolean

        Private ItemUseTick As Long

        Dim selectedx As Integer = -1
        Dim selectedy As Integer = -1
        Dim selectedw As Integer, selectedh As Integer
        Dim SelectedXCellOffset As Integer
        Dim SelectedYCellOffset As Integer
        Dim mSelectItems As Boolean
        Dim mSelectedItemID As Long
        Dim mSelectedItem As MirItemContainerItem
        Dim selectok As Boolean

        Public Property SelectItems() As Boolean
            Get
                Return mSelectItems
            End Get
            Set(ByVal value As Boolean)
                mSelectItems = value
                mSelectedItemID = 0
                mSelectedItem = New MirItemContainerItem
            End Set
        End Property

        Public ReadOnly Property SelectedItem() As MirItemContainerItem
            Get
                If mSelectedItemID <> 0 Then
                    Return mSelectedItem
                Else
                    Return Nothing
                End If
            End Get
        End Property

        Public Sub AddMouseDragItem()

            Dim tmp As MirItemContainerItem
            tmp.ItemDesc.info = GUI.MouseDragItem.MouseDragItemDesc.info
            tmp.ItemDesc.alphacol = GUI.MouseDragItem.MouseDragItemDesc.alphacol
            tmp.dimensions = GetItemSize(tmp.ItemDesc.info.Base.INT_LOOKS)
            tmp.pos.X = hoverx
            tmp.pos.Y = hovery

            For yfiller As Integer = hovery To hovery + tmp.dimensions.Height - 1
                For xfiller As Integer = hoverx To hoverx + tmp.dimensions.Width - 1

                    If bIsFilled(xfiller, yfiller) Then
                        GoTo findfreespace
                    End If

                Next
            Next

            For yfiller As Integer = hovery To hovery + tmp.dimensions.Height - 1
                For xfiller As Integer = hoverx To hoverx + tmp.dimensions.Width - 1

                    bIsFilled(xfiller, yfiller) = True

                Next
            Next

            GoTo done

findfreespace:

            For y As Integer = 0 To mMaxYVal - (tmp.dimensions.Height - 1)
                For x As Integer = 0 To 5 - (tmp.dimensions.Width - 1)

                    If Not bIsFilled(x, y) Then

                        For yfillcheck As Integer = y To y + tmp.dimensions.Height - 1
                            For xfillcheck As Integer = x To x + tmp.dimensions.Width - 1

                                If bIsFilled(xfillcheck, yfillcheck) Then GoTo nextslot

                            Next
                        Next

                        tmp.pos.X = x
                        tmp.pos.Y = y

                        For yfiller As Integer = y To y + tmp.dimensions.Height - 1
                            For xfiller As Integer = x To x + tmp.dimensions.Width - 1

                                bIsFilled(xfiller, yfiller) = True

                            Next
                        Next

                        GoTo done

                    End If

nextslot:

                Next
            Next


            MessageBoxes.AddMessageBox("InventoryErrorItemDrag", "The item (" & tmp.ItemDesc.info.Base.STR_NAME & ") could not be added to the inventory window due to insufficient free space!" & vbCrLf & "Resync is recommended (CTRL + O)", MessageBoxes.MsgBoxButtons.OK, AddressOf Game.MsgBoxResultCallback)
            Exit Sub

done:

            lstItems.Add(tmp)

            If GUI.MouseDragItem.mOrigin <> mGridType Then
                RaiseEvent AddedItem(GUI.MouseDragItem.MouseDragItemDesc.info, False)
            Else
                RaiseEvent AddedItem(GUI.MouseDragItem.MouseDragItemDesc.info, True)
            End If

            'ServerAddItem(GUI.MouseDragItem.MouseDragItemDesc.info)

        End Sub

        Public Sub ServerAddItem(ByRef item As EvoLib.Protocol.ItemInfo)

            Equipment.UnequipItem(item.INT_ID)

            'check for an existing stack, if there is one, add to it.
            Dim stack As MirItemContainerItem
            For i As Integer = 0 To lstItems.Count - 1

                stack = lstItems(i)

                If item.Base.BOOL_STACKS Then
                    If stack.ItemDesc.info.Base.INT_ID_ITEMBASE = item.Base.INT_ID_ITEMBASE Then
                        stack.ItemDesc.info.INT_COUNT += item.INT_COUNT

                        'structure holding the stack ID's to join
                        Dim req As JoinStackRequest
                        req.INT_ID_A = stack.ItemDesc.info.INT_ID
                        req.INT_ID_B = item.INT_ID

                        Dim pkt As Packet
                        pkt.PacketType = GamePacketType.JoinStacks
                        pkt.PacketOpCode = PacketOpCode.Enquiry
                        pkt.Username = Globals.sUsername

                        'send stack JoinStacks request
                        ClientSock.SendData(MakePacket(pkt, req, Globals.Crypto))

                        lstItems(i) = stack

                        Exit Sub

                    End If
                End If

            Next
            '/////////////////////////////////////////////////////////


            Dim tmp As MirItemContainerItem

            tmp.ItemDesc.info = item
            tmp.ItemDesc.alphacol = Color.FromArgb(item.INT_ALPHACOL)
            tmp.dimensions = GetItemSize(item.Base.INT_LOOKS)

            If tmp.dimensions.Width = 0 Or tmp.dimensions.Height = 0 Then Exit Sub

            For y As Integer = 0 To mMaxYVal - (tmp.dimensions.Height - 1)
                For x As Integer = 0 To 5 - (tmp.dimensions.Width - 1)

                    If Not bIsFilled(x, y) Then

                        For yfillcheck As Integer = y To y + tmp.dimensions.Height - 1
                            For xfillcheck As Integer = x To x + tmp.dimensions.Width - 1

                                If bIsFilled(xfillcheck, yfillcheck) Then GoTo nextslot

                            Next
                        Next

                        tmp.pos.X = x
                        tmp.pos.Y = y

                        For yfiller As Integer = y To y + tmp.dimensions.Height - 1
                            For xfiller As Integer = x To x + tmp.dimensions.Width - 1

                                bIsFilled(xfiller, yfiller) = True

                            Next
                        Next

                        GoTo done

                    End If

nextslot:

                Next
            Next

            MessageBoxes.AddMessageBox("InventoryError", item.Base.STR_NAME & " [ID#: " & item.INT_ID & "] could not be added to the inventory window due to insufficient free space!" & vbCrLf & "Do you want to clear the message box queue of further similar notifications?", MessageBoxes.MsgBoxButtons.YesNo, AddressOf Game.MsgBoxResultCallback)
            Exit Sub

done:

            lstItems.Add(tmp)

        End Sub

        Private Function GetItemSize(ByRef look As Integer) As Size

            Dim ret As Size
            Dim r As Integer

            ret.Width = Math.DivRem(Libs.Inventory.Image(look).Header.Width, mCellSize, r)
            If r > 0 Then ret.Width += 1

            ret.Height = Math.DivRem(Libs.Inventory.Image(look).Header.Height, mCellSize, r)
            If r > 0 Then ret.Height += 1

            Return ret

        End Function

        Public Sub New(ByRef wnd As MirWindow, ByRef name As String, ByRef x As Integer, ByRef y As Integer, _
               ByRef width As Integer, ByRef height As Integer, ByRef cellsize As Integer, ByRef maxyval As Integer, ByRef surf As Microsoft.DirectX.Direct3D.Surface, _
               ByRef tex As Microsoft.DirectX.Direct3D.Texture, ByRef gridtype As EvoLib.Protocol.ItemLocation)

            mWnd = wnd
            mName = name
            mPosX = x
            mPosY = y
            mWidth = width
            mHeight = height
            mMaxYVal = maxyval
            mCellSize = cellsize
            mSurf = surf
            mTex = tex
            mGridType = gridtype

            ReDim bIsFilled(5, mMaxYVal)

            CalcClientRect()

            mWnd.AddInterfaceItem(Me)

        End Sub

        Private Sub CalcClientRect()

            mRectGrid = New Rectangle(mPosX, mPosY, mWidth * mCellSize, mHeight * mCellSize)

        End Sub

        Public Function Contains(ByRef x As Integer, ByRef y As Integer) As Boolean Implements Windows.mWindows.GUIObj.Contains

            Return mRectGrid.Contains(x, y)

        End Function

        Public Sub Destroy() Implements Windows.mWindows.GUIObj.Destroy
            mWnd.RemoveInterfaceItem(Me)
        End Sub

        Public Sub Draw(ByRef mode As Globals.mGlobalVars.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Implements Windows.mWindows.GUIObj.Draw

            If mode = Globals.DrawingMode.Overlay Then

                DirectX.D3D.SetRenderTarget(0, mSurf)

                DirectX.D3D.BeginScene()

                DirectX.D3D.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target Or Microsoft.DirectX.Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0, 0)

                'DirectX.D3D.VertexFormat = DirectX.FVF_CUSTVERT

                DirectX.GameSprite.Begin(Microsoft.DirectX.Direct3D.SpriteFlags.AlphaBlend)
                Engine.SetDrawModeNormal(1.0, Color.White)
                DrawItems(Globals.DrawingMode.Normal)
                DirectX.GameSprite.End()

                Engine.SetDrawModeTinted(1.0, Color.White)
                DrawItems(Globals.DrawingMode.Tinted)

                Engine.UseLineShader()

                DirectX.D3D.VertexFormat = Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored.Format

                DrawItems(Globals.DrawingMode.Line)

                Engine.ResetShader()
                DirectX.D3D.EndScene()

                Engine.SetDrawModeNormal(1.0, Color.White)
                DrawItems(Globals.DrawingMode.Text)


            ElseIf mode = Globals.DrawingMode.Normal Then

                DirectX.BlitD3D(mTex, x + mPosX, y + mPosY, mCellSize * mWidth, mCellSize * mHeight, z)

            End If

        End Sub

        Private Sub DrawItems(ByRef mode As Globals.DrawingMode)

            If mode = Globals.DrawingMode.Line Then

                '===========================================
                '=-============ Highlights ===============-=
                '===========================================

                If GUI.MouseDragItem.HasMouseDragItem Then
                    If Me.Contains(Mouse.XCoord - mWnd.mPosX, Mouse.YCoord - mWnd.mPosY) Then
                        If hoverisover Then
                            'display green (free to move item to)
                            Engine.DrawSolidBox(hoverx * mCellSize, (hoverx + hoverw) * mCellSize, (hovery - mYScroll) * mCellSize, ((hovery - mYScroll) + hoverh) * mCellSize, 0.2, Color.FromArgb(128, 0, 192, 0))
                        Else
                            'display red (1 or more occupied space)
                            Engine.DrawSolidBox(hoverx * mCellSize, (hoverx + hoverw) * mCellSize, (hovery - mYScroll) * mCellSize, ((hovery - mYScroll) + hoverh) * mCellSize, 0.2, Color.FromArgb(128, 192, 0, 0))
                        End If
                    End If
                ElseIf mSelectedItemID <> 0 Then
                    If selectok Then
                        'display green (available space)
                        Engine.DrawSolidBox(selectedx * mCellSize, (selectedx + selectedw) * mCellSize, (selectedy - mYScroll) * mCellSize, ((selectedy - mYScroll) + selectedh) * mCellSize, 0.2, Color.FromArgb(128, Color.Gold))
                    Else
                        'display red (not available)
                        Engine.DrawSolidBox(selectedx * mCellSize, (selectedx + selectedw) * mCellSize, (selectedy - mYScroll) * mCellSize, ((selectedy - mYScroll) + selectedh) * mCellSize, 0.2, Color.FromArgb(128, 192, 0, 0))
                    End If
                    'End If
                Else
                    'draw hover over green highlight
                    If hoverisover Then
                        Engine.DrawSolidBox(hoverx * mCellSize, (hoverx + hoverw) * mCellSize, (hovery - mYScroll) * mCellSize, ((hovery - mYScroll) + hoverh) * mCellSize, 0.2, Color.FromArgb(128, Color.Orange))
                    End If
                End If

            End If

            If mode = Globals.DrawingMode.Normal Or mode = Globals.DrawingMode.Tinted Or mode = Globals.DrawingMode.Text Then

                Dim xoff As Integer, yoff As Integer

                For i As Integer = 0 To lstItems.Count - 1

                    With lstItems(i)

                        If .pos.Y + .dimensions.Height >= mYScroll And .pos.Y < mYScroll + mHeight Then

                            If Not (Mouse.MouseObjType = Mouse.MouseObjTypes.Inventory And Mouse.MouseObjIndex = i) Then

                                xoff = (((.dimensions.Width * mCellSize) - Libs.Inventory.Image(.ItemDesc.info.Base.INT_LOOKS).Header.Width) \ 2)
                                yoff = (((.dimensions.Height * mCellSize) - Libs.Inventory.Image(.ItemDesc.info.Base.INT_LOOKS).Header.Height) \ 2)

                                If mode = Globals.DrawingMode.Normal Then
                                    Libs.Inventory.Image(.ItemDesc.info.Base.INT_LOOKS).Draw((.pos.X * mCellSize) + xoff, ((.pos.Y - mYScroll) * mCellSize) + yoff, 0.1)
                                ElseIf mode = Globals.DrawingMode.Tinted Then
                                    DirectX.GameSprite.Begin(Microsoft.DirectX.Direct3D.SpriteFlags.AlphaBlend)
                                    Engine.SetDrawModeTinted(1.0, .ItemDesc.alphacol)
                                    Libs.Inventory.Image(.ItemDesc.info.Base.INT_LOOKS).DrawAlpha((.pos.X * mCellSize) + xoff, ((.pos.Y - mYScroll) * mCellSize) + yoff, 0.1)
                                    DirectX.GameSprite.End()
                                ElseIf mode = Globals.DrawingMode.Text Then
                                    If .ItemDesc.info.Base.BOOL_STACKS Then
                                        DirectX.ProFont9.DrawBorderText(.ItemDesc.info.INT_COUNT, ((.pos.X) * mCellSize) + xoff, (((.pos.Y) - mYScroll) * mCellSize) + yoff, Color.White, Color.Black, 0.1)
                                    End If
                                End If

                            End If
                        End If

                    End With

                Next


            End If


        End Sub

        Public Sub Focus() Implements Windows.mWindows.GUIObj.Focus

        End Sub

        Public ReadOnly Property GetsFocus() As Boolean Implements Windows.mWindows.GUIObj.GetsFocus
            Get

            End Get
        End Property

        Public Sub KeyboardInput(ByRef type As Keyboard.mKeyboardVars.KeyboardInputType, ByRef e As System.Windows.Forms.KeyEventArgs) Implements Windows.mWindows.GUIObj.KeyboardInput

        End Sub

        Public Sub KeyboardInput(ByRef type As Keyboard.mKeyboardVars.KeyboardInputType, ByRef e As System.Windows.Forms.KeyPressEventArgs) Implements Windows.mWindows.GUIObj.KeyboardInput

        End Sub

        Public Sub MouseInput(ByRef type As Mouse.mMouseVars.MouseInputType, ByRef e As System.Windows.Forms.MouseEventArgs) Implements Windows.mWindows.GUIObj.MouseInput

            If type = Mouse.MouseInputType.MouseClick And GUI.MouseDragItem.HasMouseDragItem Then

                If ItemUseTick + 300000 > DateTime.Now.Ticks Then

                    GUI.MouseDragItem.ReturnMouseDragItem()
                    GUI.MouseDragItem.RemoveMouseDragItem()

                    Player.UseItem(MouseDragItem.MouseDragItemDesc.info.INT_ID)

                    Exit Sub

                End If

                CalcHover(e)

                If hoverisover Then

                    'place the mousedragitem into the item container
                    AddMouseDragItem()
                    'clear the mousedragitem reference
                    GUI.MouseDragItem.RemoveMouseDragItem()

                Else

                    For Each I As MirItemContainerItem In lstItems
                        'if this item is the one we are hovering over
                        If I.pos.X = hoverx And I.pos.Y = hovery Then
                            'check the item we are hovering over is the same as the mousedragitem
                            If I.ItemDesc.info.Base.STR_NAME = GUI.MouseDragItem.MouseDragItemDesc.info.Base.STR_NAME Then

                                'structure holding the stack ID's to join
                                Dim req As JoinStackRequest
                                req.INT_ID_A = GUI.MouseDragItem.MouseDragItemDesc.info.INT_ID
                                req.INT_ID_B = I.ItemDesc.info.INT_ID

                                Dim pkt As Packet
                                pkt.PacketType = GamePacketType.JoinStacks
                                pkt.PacketOpCode = PacketOpCode.Enquiry
                                pkt.Username = Globals.sUsername

                                'send stack JoinStacks request
                                ClientSock.gSock.SendData(MakePacket(pkt, req, Globals.Crypto))

                                GUI.MouseDragItem.RemoveMouseDragItem()

                            End If
                            Exit For
                        End If
                    Next

                End If


                'RemoveMouseDragItem()
                'ElseIf type = Mouse.MouseInputType.MouseMove And GUI.MouseDragItem.HasMouseDragItem Then

                'ElseIf type = Mouse.MouseInputType.MouseMove And Mouse.MouseObjIndex <> -1 Then

                'Dim x As Integer = e.X - (XOffset + (Int(mousexoffset / cCellSize) * cCellSize)) + Libs.Inventory.Image(lstItems(Mouse.MouseObjIndex).info.INT_LOOKS).Header.OffsetX '+ (cCellSize \ 2)
                'Dim y As Integer = e.Y - (YOffset + (Int(mouseyoffset / cCellSize) * cCellSize)) + Libs.Inventory.Image(lstItems(Mouse.MouseObjIndex).info.INT_LOOKS).Header.OffsetY '+ (cCellSize \ 2)


                

            Else

                'If type = Mouse.MouseInputType.MouseUp Then

                'hasmoved = False

                'ElseIf Mouse.bIsDown And type = Mouse.MouseInputType.MouseMove Then

                'hasmoved = True

                Dim x As Integer = e.X - mPosX
                Dim y As Integer = e.Y - mPosY

                x = Fix(x / mCellSize)
                y = Fix(y / mCellSize) + mYScroll

                If bIsFilled(x, y) Then

                    For i As Integer = 0 To lstItems.Count - 1

                        If i >= lstItems.Count Then Exit Sub

                        With lstItems(i)

                            If x >= .pos.X And x < .pos.X + .dimensions.Width Then
                                If y >= .pos.Y And y < .pos.Y + .dimensions.Height Then

                                    If type = Mouse.MouseInputType.MouseClick Then 'And Not hasmoved Then


                                        'Check if we are hovering over an empty spot

                                        If mSelectItems Then

                                            Dim xoff As Integer = (((.dimensions.Width * mCellSize) - Libs.Inventory.Image(.ItemDesc.info.Base.INT_LOOKS).Header.Width) \ 2)
                                            Dim yoff As Integer = (((.dimensions.Height * mCellSize) - Libs.Inventory.Image(.ItemDesc.info.Base.INT_LOOKS).Header.Height) \ 2)

                                            Dim MouseObjXOffset As Integer = e.X - ((.pos.X * mCellSize) + mPosX + xoff)
                                            Dim MouseObjYOffset As Integer = e.Y - (((.pos.Y - mYScroll) * mCellSize) + mPosY + yoff)

                                            SelectedXCellOffset = e.X - ((.pos.X * mCellSize) + mPosX) '+ Libs.Inventory.Image(.info.INT_LOOKS).Header.OffsetX
                                            SelectedYCellOffset = e.Y - (((.pos.Y - mYScroll) * mCellSize) + mPosY) '+ Libs.Inventory.Image(.info.INT_LOOKS).Header.OffsetY

                                            mSelectedItemID = .ItemDesc.info.INT_ID
                                            mSelectedItem = lstItems(i)

                                            CalcHover(e)

                                            If hoverisover Then

                                                mSelectedItemID = .ItemDesc.info.INT_ID
                                                selectedx = hoverx
                                                selectedy = hovery
                                                selectedw = hoverw
                                                selectedh = hoverh
                                                selectok = Inventory.lstAllow.Contains(.ItemDesc.info.Base.BYTE_TYPE)


                                                RaiseEvent OnSelectedItemChange()

                                            Else

                                                mSelectedItem = New MirItemContainerItem
                                                mSelectedItemID = 0

                                            End If

                                        Else

                                            If e.Button = MouseButtons.Left Then

                                                'Dim anim As New GUI.Animation(ImgLibs.Inventory, .info.INT_LOOKS, 1, 100000000)
                                                'mMouseItem.StartAnim(anim, True, .alphacol.ToArgb)
                                                'mMouseItem.Visible = True

                                                Dim xoff As Integer = (((.dimensions.Width * mCellSize) - Libs.Inventory.Image(.ItemDesc.info.Base.INT_LOOKS).Header.Width) \ 2)
                                                Dim yoff As Integer = (((.dimensions.Height * mCellSize) - Libs.Inventory.Image(.ItemDesc.info.Base.INT_LOOKS).Header.Height) \ 2)

                                                Dim MouseObjXOffset As Integer = e.X - ((.pos.X * mCellSize) + mPosX + xoff)
                                                Dim MouseObjYOffset As Integer = e.Y - (((.pos.Y - mYScroll) * mCellSize) + mPosY + yoff)

                                                Dim MouseObjXCellOffset As Integer = e.X - ((.pos.X * mCellSize) + mPosX) '+ Libs.Inventory.Image(.info.INT_LOOKS).Header.OffsetX
                                                Dim MouseObjYCellOffset As Integer = e.Y - (((.pos.Y - mYScroll) * mCellSize) + mPosY) '+ Libs.Inventory.Image(.info.INT_LOOKS).Header.OffsetY

                                                'mMouseItem.SetPos(e.X - Mouse.MouseObjXOffset, e.Y - Mouse.MouseObjYOffset)

                                                'hoverw = lstItems(i).dimensions.Width
                                                'hoverh = lstItems(i).dimensions.Height

                                                'Windows.FocusWindow(InventoryWindow)
                                                'Windows.FocusInterface(mMouseItem)

                                                'Mouse.MouseObj = mMouseItem
                                                'Mouse.MouseObjIndex = i
                                                'Mouse.MouseObjType = Mouse.MouseObjTypes.Inventory

                                                If GameWindow.isShiftDown And .ItemDesc.info.Base.BOOL_STACKS Then

                                                    Inventory.SplittingStack = .ItemDesc.info.INT_ID
                                                    MessageBoxes.AddMessageBox("SPLITSTACK", "Enter the amount:", MessageBoxes.mMsgBoxVars.MsgBoxButtons.Input, AddressOf Game.MsgBoxResultCallback, , , "0123456789")

                                                Else

                                                    GUI.MouseDragItem.SetMouseDragItem(.ItemDesc, Me, mGridType, MouseObjXOffset, MouseObjYOffset, MouseObjXCellOffset, MouseObjYCellOffset)
                                                    ItemUseTick = DateTime.Now.Ticks

                                                    RemoveItemFromFilledTable(i)
                                                    RaiseEvent RemovedItem(.ItemDesc.info)

                                                    lstItems.RemoveAt(i)

                                                    GameWindow.HideTooltip()

                                                End If

                                            ElseIf e.Button = MouseButtons.Right Then


                                                Player.UseItem(.ItemDesc.info.INT_ID)

                                            End If
                                        End If

                                    ElseIf type = Mouse.MouseInputType.MouseDoubleClick Then

                                        Player.UseItem(.ItemDesc.info.INT_ID)



                                    ElseIf type = Mouse.MouseInputType.MouseMove Then

                                        CalcHover(e)

                                        GameWindow.SetItemTooltip(.ItemDesc.info)

                                        Exit Sub

                                    End If
                                End If
                            End If

                        End With

                    Next

                Else

                    If type = Mouse.MouseInputType.MouseMove Then

                        CalcHover(e)

                        Exit Sub

                    End If

                    GameWindow.HideTooltip()

                End If

            End If

        End Sub

        Public Sub CalcHover(e As System.Windows.Forms.MouseEventArgs)

            Dim r As Integer
            Dim w As Integer
            Dim h As Integer


            Dim x As Integer = e.X - mPosX
            Dim y As Integer = e.Y - mPosY

            x = Fix(x / mCellSize)
            y = (Fix(y / mCellSize)) + mYScroll

            'Debug.Print("Before X:" & x & " Y:" & y & " XO:" & Mouse.MouseObjXOffset & " YO:" & Mouse.MouseObjYOffset)

            If GUI.MouseDragItem.HasMouseDragItem Then

                w = Math.DivRem(Libs.Inventory.Image(GUI.MouseDragItem.MouseDragItemDesc.info.Base.INT_LOOKS).Header.Width, mCellSize, r)
                If r > 0 Then w += 1
                h = Math.DivRem(Libs.Inventory.Image(GUI.MouseDragItem.MouseDragItemDesc.info.Base.INT_LOOKS).Header.Height, mCellSize, r)
                If r > 0 Then h += 1

                x = x - Fix(GUI.MouseDragItem.CellXOffset / mCellSize)
                y = y - Fix(GUI.MouseDragItem.CellYOffset / mCellSize)

            ElseIf mSelectItems Then

                w = Math.DivRem(Libs.Inventory.Image(SelectedItem.ItemDesc.info.Base.INT_LOOKS).Header.Width, mCellSize, r)
                If r > 0 Then w += 1
                h = Math.DivRem(Libs.Inventory.Image(SelectedItem.ItemDesc.info.Base.INT_LOOKS).Header.Height, mCellSize, r)
                If r > 0 Then h += 1

                x = x - Fix(SelectedXCellOffset / mCellSize)
                y = y - Fix(SelectedYCellOffset / mCellSize)
                'x = x - Fix(GUI.MouseDragItem.CellXOffset / mCellSize)
                'y = y - Fix(GUI.MouseDragItem.CellYOffset / mCellSize)

            Else

                h = 1 : w = 1

            End If

            'Debug.Print("After X:" & x & " Y:" & y)

            hoverx = x
            hovery = y
            hoverisover = True

            'If bIsFilled(x, y) Then

            Dim tmpx As Integer
            Dim tmpy As Integer

            hoverw = w
            hoverh = h

            For tmpx = x To x + w - 1
                For tmpy = y To y + h - 1
                    If GUI.MouseDragItem.HasMouseDragItem Then
                        If (tmpx >= 0 And tmpx < mWidth And tmpy >= 0 And tmpy <= mMaxYVal) Then
                            If bIsFilled(tmpx, tmpy) Then
                                hoverisover = False
                                Exit Sub
                            End If
                        Else
                            hoverisover = False
                            Exit Sub
                        End If
                    End If
                Next
            Next

        End Sub

        Public ReadOnly Property ParentWin() As MirWindow Implements Windows.mWindows.GUIObj.ParentWin
            Get
                Return mWnd
            End Get
        End Property

        Public Sub SetPos(ByRef x As Integer, ByRef y As Integer) Implements Windows.mWindows.GUIObj.SetPos
            mPosX = x
            mPosY = y
            CalcClientRect()
        End Sub

        Public Event TabSwitch() Implements Windows.mWindows.GUIObj.TabSwitch

        Public Sub UnFocus() Implements Windows.mWindows.GUIObj.UnFocus

        End Sub

        Private Sub RemoveItemFromFilledTable(ByRef index As Integer)

            If index >= lstItems.Count Then Exit Sub
            If index < 0 Then Exit Sub

            With lstItems(index)

                For x As Integer = .pos.X To .pos.X + .dimensions.Width - 1
                    For y As Integer = .pos.Y To .pos.Y + .dimensions.Height - 1

                        bIsFilled(x, y) = False

                    Next
                Next

            End With

        End Sub

        Public ReadOnly Property Name() As String Implements Windows.mWindows.GUIObj.Name
            Get
                Return mName
            End Get
        End Property

        Public Sub ReloadSurafces() Implements Windows.mWindows.GUIObj.ReloadSurafces
            RaiseEvent RefreshingSurfaces(mSurf, mTex)
        End Sub

        Public Sub Remove(ByVal itemid As Int64)

            Dim item As MirItemContainerItem

            For i As Integer = 0 To lstItems.Count - 1

                item = lstItems(i)

                If item.ItemDesc.info.INT_ID = itemid Then

                    RemoveItemFromFilledTable(i)
                    RaiseEvent RemovedItem(item.ItemDesc.info)
                    lstItems.Remove(item)
                    Exit Sub

                End If

            Next

        End Sub

    End Class

End Namespace